package com.juku2024.juku.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.juku2024.juku.R
import com.juku2024.juku.VideoListActivity
import com.juku2024.juku.adapter.MoreAdapter
import com.juku2024.juku.base.BaseFragment
import com.juku2024.juku.db.entity.UnlockMoreModel
import com.juku2024.juku.model.DebLockingMoreEntity
import com.juku2024.juku.model.Genre
import com.juku2024.juku.network.retrofit.OnRetrofitResultListener
import com.juku2024.juku.tools.WrapLinearLayoutManager
import com.juku2024.juku.viewmodel.UnlockViewModel
import com.juku2024.juku.widget.LoadingView

class MoreFragment: BaseFragment() {

    private var data: List<DebLockingMoreEntity>? = null
    private val adapter by lazy { MoreAdapter(ArrayList()) }
    private val viewModel by lazy { getViewModel(requireActivity().viewModelStore, UnlockViewModel::class.java) }

    private lateinit var recycler: RecyclerView
    private lateinit var loadingView: LoadingView

    override fun layoutView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.fragment_more, container, false)
    }

    override fun initView(rootView: View) {
        super.initView(rootView)
        recycler = rootView.findViewById(R.id.recycler_more)
        loadingView = rootView.findViewById(R.id.loadingView_more)

        recycler.layoutManager = WrapLinearLayoutManager(requireContext())
        recycler.setHasFixedSize(true)
        recycler.adapter = adapter
    }


    override fun bindEvent() {
        super.bindEvent()
        adapter.setOnItemClickListener { _, _, position ->
            val item = adapter.data[position]
            val genre = Genre(
                item.name,
                item.id.toLong(),
                item.genre_id
            )
            VideoListActivity.toCategory(requireContext(), genre)
        }
    }


    override fun onResume() {
        super.onResume()
        if (data.isNullOrEmpty()) {
            loadingView.showLoading()
            doHttpUnlockMore()
            return
        }
        adapter.setList(data)
    }

    private fun doHttpUnlockMore() {
        viewModel.getUnlockList(object: OnRetrofitResultListener<UnlockMoreModel> {
            override fun onSuccess(t: UnlockMoreModel) {
                if (isFragmentNotActive()) return
                loadingView.hideLoading()
                if (t.data.isNullOrEmpty()) {
                    loadingView.showEmptyStatus("当前没有可用的解锁分类!")
                    return
                }
                data = t.data
                adapter.setList(data)
            }

            override fun onFailed(msg: String) {
                if (isFragmentNotActive()) return
                loadingView.hideLoading()
                loadingView.showErrorStatus(
                    getString(R.string.error_net),
                    getString(R.string.retry)
                ) {
                    loadingView.showLoading()
                    loadingView.postDelayed({
                        if (isFragmentNotActive()) return@postDelayed
                        doHttpUnlockMore()
                    }, 1000)
                }
            }
        })
    }

}